package com.airena.config;

import com.airena.interceptor.JwtTokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;

    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
//        管理端
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/login",                        // 登录接口
                        "/register",                     // 注册接口
                        "/doc.html/**",                  // Knife4j文档页面
                        "/swagger-ui.html/**",           // Swagger页面（备用）
                        "/webjars/**",                   // 静态资源
                        "/swagger-resources/**",         // swagger-resources
                        "/v2/api-docs/**",               // Swagger2接口文档
                        "/v3/api-docs/**",               // OpenAPI3接口文档
                        "/favicon.ico"                   // 网站图标
                );
    }

    /**
     * 设置静态资源映射,访问接口文档
     *
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源映射");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
